//    Copyright (c) 2009 Elwyn Malethan
//
//    This file is part of SeemoreJ.
//
//    SeemoreJ is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    SeemoreJ is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.

//    You should have received a copy of the GNU General Public License
//    along with SeemoreJ.  If not, see <http://www.gnu.org/licenses/>.

package com.malethan.seemorej.taglib;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.taglibs.standard.lang.jstl.ELEvaluator;
import org.apache.taglibs.standard.lang.jstl.ELException;
import org.apache.taglibs.standard.lang.jstl.JSTLVariableResolver;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.io.IOException;

import com.malethan.seemorej.taglib.elements.BaseElement;

public abstract class ElementTag extends BodyTagSupport {
    //----------------------------------------------------------------------- Static Properties and Constants

    private static final Log log = LogFactory.getLog(ElementTag.class);

    //----------------------------------------------------------------------- Static Methods
    //----------------------------------------------------------------------- Instance Properties

    String htmlId;
    String htmlClass;
    String name;
    Object value;

    //----------------------------------------------------------------------- Constructors
    //----------------------------------------------------------------------- Getters and Setters

    public void setName(String name) {
        this.name = name;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void setHtmlId(String htmlId) {
        this.htmlId = htmlId;
    }

    public void setHtmlClass(String htmlClass) {
        this.htmlClass = htmlClass;
    }

    //----------------------------------------------------------------------- Instance Methods

    protected abstract String constructElement();

    protected void setCommonAttributes(BaseElement element) {
        element.setHtmlId(htmlId);
        element.setHtmlClass(htmlClass);
        element.setName(name);
        element.setValue(value);
    }

    public int doStartTag() throws JspException {
        if (value == null ) {
            ELEvaluator evaluator = new ELEvaluator(new JSTLVariableResolver());
            try {
                log.debug("Looking up value for '" + name + "'");
                value = evaluator.evaluate("${" + name + "}", pageContext, Object.class, null, null);
            } catch (ELException e1) {
                log.warn("Encountered problems looking up '" + name + "'", e1);
            }
        }
        String html = constructElement();
        try {
            pageContext.getOut().print(html);
        } catch (IOException e) {
            throw new JspException("Failed to render '" + html + "'", e);
        } finally {
            release();
        }
        return SKIP_BODY;
    }

    public void release() {
        super.release();
        this.htmlClass = null;
        this.htmlId = null;
        this.name = null;
        this.value = null;
    }
}
